package com.bookmarket.controller;

import com.bookmarket.entity.Custom;
import com.bookmarket.service.HJHStageLoginAndRegService;
import com.bookmarket.utils.GetMsesageCode;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/stageregist")
public class HJHStageRegistAndLoginController {

    @Resource
    private HJHStageLoginAndRegService hJHStageLoginAndRegService;

    //注册时检查用户是否存在
    @RequestMapping("/CheckName")
    public String CheckName(Custom custom){
        //System.out.println(custom.getName()+"*********");
        Custom custom1=hJHStageLoginAndRegService.CheckName(custom);
        if (custom1!=null){
            return "true";
        }
        return "false";
    }

    @RequestMapping("/sendSMS")
    public String sendSMS(String phone)  {
        //获取到浏览器页面传递过来的参数（手机号）

        //根据获取到的手机号发送验证码
        Object code= GetMsesageCode.getCode(phone);//获取状态
        //System.out.println(code);
        return code.toString();
    }

    @RequestMapping("/toRegist")
    public ModelAndView toRegist(){
        ModelAndView mv=new ModelAndView();
        mv.setViewName("stage/HJHRegist");
        return mv;
    }



    //用户登录
    @RequestMapping("/CustomLogin")
    public String CustomLogin(Custom custom,HttpSession session){
        Map<String,Object> map=new HashMap<>();
        Custom custom1=hJHStageLoginAndRegService.CustomLogin(custom);
        if (custom1==null){
            return "false";
        }else{
            session.setAttribute("NowCustom",custom1);
            return "true";
        }
    }

    //用户注册
    @RequestMapping("/CustomRegist")
    public ModelAndView CustomRegist(Custom custom){
        ModelAndView mv=new ModelAndView();
        hJHStageLoginAndRegService.CustomRegist(custom);
        mv.setViewName("stage/HJHStageLogin");
        return mv;
    }

    //跳首页
    @RequestMapping("/toIndex")
    public ModelAndView toIndex(){
        ModelAndView mv = new ModelAndView();
        mv.setViewName("stage/index");
        return mv;
    }

    //跳登录页
    @RequestMapping("/toLogin")
    public ModelAndView toLogin(){
        ModelAndView mv = new ModelAndView();
        mv.setViewName("stage/HJHStageLogin");
        return mv;
    }
    //跳登录页
    @RequestMapping("/toPersonal")
    public ModelAndView toPersonal(){
        ModelAndView mv = new ModelAndView();
        mv.setViewName("stage/LLpersonal.html");
        return mv;
    }

    //注销方法
    @RequestMapping("/outLogin")
    public ModelAndView outLogin(HttpSession session){
        ModelAndView mv = new ModelAndView();
        //通过session.invalidata()方法来注销当前的session
        session.removeAttribute("NowCustom");
        mv.setViewName("stage/index");
        return mv;
    }


    public HJHStageLoginAndRegService gethJHStageLoginAndRegService() {
        return hJHStageLoginAndRegService;
    }

    public void sethJHStageLoginAndRegService(HJHStageLoginAndRegService hJHStageLoginAndRegService) {
        this.hJHStageLoginAndRegService = hJHStageLoginAndRegService;
    }
}